import Vue from 'vue'
import Router from 'vue-router'
import Login from './views/Login.vue'
import TemplateHome from './components/Templatehome.vue';

Vue.use(Router)


const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },{
      path: '/',
      name: 'Manage',
      component: TemplateHome,
      redirect: '/GymManage',
      children: [
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        { path: 'GymManage',name:'GymManage', component: () => import(/* webpackChunkName: "about" */ './views/GymManage.vue') },
        {path: 'UserManage', component: () => import(/* webpackChunkName: "about" */ './views/UserManage.vue')},
        {path: 'ViewManage', component: () => import(/* webpackChunkName: "about" */ './views/ViewManage.vue')}
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
    if (to.path == '/Login') {
        sessionStorage.removeItem('user');
    }
    let result = sessionStorage.getItem('user');
    if (!result && to.path != '/Login') {
        next({ path: '/Login' })
    } else {
        next()
    }
    next()
});
export default router;